package com.bygf.curve.panel;

import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JColorChooser;

/**
 * 属性对话框，用于修改曲线界面参数
 *
 * @author sunyupei
 */
public class PropertyDialog extends javax.swing.JDialog {

    //曲线整体对象
    ChartPanel chartPanel;
    //界面实例
    private static PropertyDialog dialog;

    /**
     * Creates new form PropertyDialog
     */
    public PropertyDialog(ChartPanel chartPanel) {
        initComponents();
        this.getContentPane().setBackground(Color.WHITE);
        this.chartPanel = chartPanel;
        init();
    }

    /**
     * 界面初始化
     */
    private void init() {
        bgColorText.setBackground(chartPanel.getCurvePanel().getBackColor());
        showGridBox.setSelected(chartPanel.getCurvePanel().isGridVisible());
        gridColorText.setBackground(chartPanel.getCurvePanel().getGridColor());
        y1BeginText.setText(chartPanel.getYAxis().getStartValue() + "");
        y1EndText.setText(chartPanel.getYAxis().getEndValue() + "");
        if (chartPanel.isY2AxisVisible()) {
            y2BeginText.setText(chartPanel.getY2Axis().getStartValue() + "");
            y2EndText.setText(chartPanel.getY2Axis().getEndValue() + "");
        } else {
            y2BeginText.setEnabled(false);
            y2EndText.setEnabled(false);
        }
    }

    /**
     * 获取界面实例
     * @param chartPanel
     * @return 
     */
    public static PropertyDialog getInstance(ChartPanel chartPanel) {
        if (dialog != null) {
            dialog.dispose();
        }
        dialog = new PropertyDialog(chartPanel);
        return dialog;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel6 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        bgColorText = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        showGridBox = new javax.swing.JCheckBox();
        jLabel8 = new javax.swing.JLabel();
        gridColorText = new javax.swing.JTextField();
        jPanel9 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        y1BeginText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        y1EndText = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        y2BeginText = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        y2EndText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("属性");
        setAlwaysOnTop(true);
        setIconImage(null);
        setIconImages(null);
        setResizable(false);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("画布"));
        jPanel6.setOpaque(false);

        jLabel7.setText("背景色：");

        bgColorText.setBackground(new java.awt.Color(0, 0, 0));
        bgColorText.setEnabled(false);
        bgColorText.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                bgColorTextMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bgColorText, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel7)
                .addComponent(bgColorText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("网格线"));
        jPanel7.setOpaque(false);

        showGridBox.setText("显示");
        showGridBox.setOpaque(false);
        showGridBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                showGridBoxItemStateChanged(evt);
            }
        });

        jLabel8.setText("颜色：");

        gridColorText.setBackground(new java.awt.Color(0, 0, 0));
        gridColorText.setEnabled(false);
        gridColorText.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                gridColorTextMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(showGridBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gridColorText, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(showGridBox)
                .addComponent(jLabel8)
                .addComponent(gridColorText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("坐标轴"));
        jPanel9.setEnabled(false);
        jPanel9.setOpaque(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("纵轴1范围"));
        jPanel2.setOpaque(false);

        jLabel3.setText("起点：");

        y1BeginText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                y1BeginTextKeyPressed(evt);
            }
        });

        jLabel4.setText("终点：");

        y1EndText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                y1EndTextKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(y1BeginText, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(y1EndText, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(y1BeginText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4)
                .addComponent(y1EndText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("纵轴2范围"));
        jPanel8.setOpaque(false);

        jLabel9.setText("起点：");

        y2BeginText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                y2BeginTextKeyPressed(evt);
            }
        });

        jLabel10.setText("终点：");

        y2EndText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                y2EndTextKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(y2BeginText, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(y2EndText, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel9)
                .addComponent(y2BeginText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel10)
                .addComponent(y2EndText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 9, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bgColorTextMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bgColorTextMouseReleased
        Color c = JColorChooser.showDialog(this, "颜色选择", bgColorText.getBackground());
        if (c != null) {
            bgColorText.setBackground(c);
            chartPanel.getCurvePanel().setBackColor(c);
            chartPanel.getCurvePanel().repaint();
        }
    }//GEN-LAST:event_bgColorTextMouseReleased

    private void gridColorTextMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gridColorTextMouseReleased
        Color c = JColorChooser.showDialog(this, "颜色选择", gridColorText.getBackground());
        if (c != null) {
            gridColorText.setBackground(c);
            chartPanel.getCurvePanel().setGridColor(c);
            chartPanel.getCurvePanel().repaint();
        }
    }//GEN-LAST:event_gridColorTextMouseReleased

    private void showGridBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_showGridBoxItemStateChanged
        chartPanel.getCurvePanel().setGridVisible(showGridBox.isSelected());
        chartPanel.getCurvePanel().repaint();
    }//GEN-LAST:event_showGridBoxItemStateChanged

    private void y1BeginTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_y1BeginTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                float value = Float.parseFloat(y1BeginText.getText().trim());
                chartPanel.getYAxis().setStartValue(value);
                chartPanel.getCurvePanel().repaint();
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_y1BeginTextKeyPressed

    private void y1EndTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_y1EndTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                float value = Float.parseFloat(y1EndText.getText().trim());
                chartPanel.getYAxis().setEndValue(value);
                chartPanel.getCurvePanel().setAutoAxis(false);
                chartPanel.getCurvePanel().repaint();
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_y1EndTextKeyPressed

    private void y2BeginTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_y2BeginTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                float value = Float.parseFloat(y2BeginText.getText().trim());
                chartPanel.getY2Axis().setStartValue(value);
                chartPanel.getCurvePanel().setAutoAxis(false);
                chartPanel.getCurvePanel().repaint();
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_y2BeginTextKeyPressed

    private void y2EndTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_y2EndTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                float value = Float.parseFloat(y2EndText.getText().trim());
                chartPanel.getY2Axis().setEndValue(value);
                chartPanel.getCurvePanel().setAutoAxis(false);
                chartPanel.getCurvePanel().repaint();
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_y2EndTextKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField bgColorText;
    private javax.swing.JTextField gridColorText;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JCheckBox showGridBox;
    private javax.swing.JTextField y1BeginText;
    private javax.swing.JTextField y1EndText;
    private javax.swing.JTextField y2BeginText;
    private javax.swing.JTextField y2EndText;
    // End of variables declaration//GEN-END:variables

    void setpropVisible(boolean propVisible) {
        jPanel9.setVisible(propVisible);
        this.setSize(this.getWidth(), 160);
        this.repaint();
    }
}
